package com.zgscwjm.taobaphelper.xp;


import android.content.ContentResolver;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * Created by fukunaka on 2017/3/17.
 */

public abstract class DSTopManager {
    protected static String CR_CLS_NAME = ContentResolver.class.getName();
    protected static String FLOAT_PRIMARY_CLS = Float.TYPE.getName();
    protected static String INT_PRIMARY_CLS = Integer.TYPE.getName();
    protected static String LONG_PRIMARY_CLS = Long.TYPE.getName();
    protected static String STR_CLS = String.class.getName();
    public static final String TAG = "DSTelephoneHacker";
    static HMethods[] needToModifyFields = new HMethods[]{HMethods.getSimSerialNumber, HMethods.getSubscriberId, HMethods.getLine1Number, HMethods.hasIccCard, HMethods.getSimState, HMethods.getDeviceId, HMethods.getSimCountryIso, HMethods.getNetworkCountryIso, HMethods.getNetworkOperator, HMethods.getNetworkOperatorName, HMethods.getNetworkType, HMethods.getPhoneType, HMethods.getMacAddress, HMethods.getSSID, HMethods.getBSSID, HMethods.isWifiEnabled, HMethods.getWifiState, HMethods.getRadioVersion};

     static class AnonymousClass1 {
      public static final  int[] $SwitchMap$jp$jun_nama$test$utf7ime$xposed$DSTopManager$HMethods = new int[HMethods.values().length];
    }



    public enum HMethods {
        getSimSerialNumber,
        getSubscriberId,
        getLine1Number,
        getSimCountryIso,
        getSimOperator,
        getSimOperatorName,
        getNetworkCountryIso,
        getNetworkOperator,
        getNetworkOperatorName,
        getNetworkType,
        getPhoneType,
        hasIccCard,
        getSimState,
        getDeviceId,
        getNeighboringCellInfo,
        getMacAddress,
        getSSID,
        getBSSID,
        getConnectionInfo,
        isWifiEnabled,
        getWifiState,
        get,
        getString,
        getFloat,
        getInt,
        getLong,
        put,
        putString,
        putFloat,
        putInt,
        putLong,
        DISPLAY,
        getInstalledPackages,
        getInstalledApplications,
        getRecentTasks,
        getRadioVersion,
        getWidth,
        getHeight,
        getLatitude,
        getLongitude,
        LOCATION,
        getLac,
        getCid,
        getNetworkId,
        getBaseStationId,
        BRAND,
        MODEL,
        PRODUCT,
        MANUFACTURER,
        DEVICE,
        CPU_ABI,
        CPU_ABI2,
        FINGERPRINT,
        HARDWARE,
        RELEASE,
        SDK,
        ANDROID_ID
    }

    public enum Heihei {
    }

    public class DSBaseHooker  extends XC_MethodHook {
        protected XC_LoadPackage.LoadPackageParam lpp;
        protected HMethods m;

        public DSBaseHooker(HMethods hMethods, XC_LoadPackage.LoadPackageParam loadPackageParam) {
            this.m = hMethods;
            this.lpp = loadPackageParam;
            DSLogger.info(" call from lpp " + loadPackageParam.packageName, new Object[0]);
        }

        protected void afterHookedMethod(MethodHookParam methodHookParam) {
            int[] iArr = AnonymousClass1.$SwitchMap$jp$jun_nama$test$utf7ime$xposed$DSTopManager$HMethods;
            this.m.ordinal();
        }

        protected void beforeHookedMethod(MethodHookParam methodHookParam) {
        }
    }

    class DSEmptyListHooker extends DSBaseHooker {
        public DSEmptyListHooker(HMethods hMethods, XC_LoadPackage.LoadPackageParam loadPackageParam) {
            super(hMethods, loadPackageParam);
        }

        protected void afterHookedMethod(MethodHookParam methodHookParam) {
            String str = this.lpp.packageName;
            DSLogger.info("request from package:" + this.lpp.packageName, new Object[0]);
            if (str.equals(getClass().getPackage())) {
                DSLogger.info(" request from current package with " + getClass(), new Object[0]);
            } else {
                methodHookParam.setResult(new ArrayList());
            }
        }
    }

    public class DSPropertyHooker extends DSBaseHooker {
        public DSPropertyHooker(HMethods hMethods, XC_LoadPackage.LoadPackageParam loadPackageParam) {
            super(hMethods, loadPackageParam);
        }

        protected void afterHookedMethod(MethodHookParam methodHookParam) {
            Object fetchValue = DSParams.fetchValue(this.m.name());
            if (this.m == HMethods.get && methodHookParam.args.length > 0 && methodHookParam.args[0] != null) {
                methodHookParam.args[0].toString();
                HMethods.ANDROID_ID.name();
            }
            DSLogger.info("top manager [%s] with method [" + methodHookParam.method + "]", this.m.name());
            if (methodHookParam.args != null) {
                DSLogger.info(" p args :" + StringUtils.join(methodHookParam.args), new Object[0]);
            }
            Log.i(DSLogger.TAG, "call for[" + this.m.name() + "] with fetch from origin [" + methodHookParam.getResult() + "] mock value [" + fetchValue + "]");
            if (fetchValue == null) {
                super.afterHookedMethod(methodHookParam);
            } else {
                methodHookParam.setResult(fetchValue);
            }
        }
    }

    protected DSTopManager() {
    }

    public abstract void addAllHook(XC_LoadPackage.LoadPackageParam loadPackageParam);

    protected void addHook(String str, ClassLoader classLoader, HMethods hMethods, XC_MethodHook xC_MethodHook, Object... objArr) {
        if (objArr == null || objArr.length == 0) {
            try {
                XposedHelpers.findAndHookMethod(str, classLoader, hMethods.name(), xC_MethodHook);
                return;
            } catch (Throwable e) {
                Log.i("DSTelephoneHacker", "couldn't hook method " + hMethods);
                DSLogger.error(e, e.getMessage(), new Object[0]);
                return;
            }
        }
        try {
            XposedHelpers.findAndHookMethod(str, classLoader, hMethods.name(), ArrayUtils.add(objArr, (Object) xC_MethodHook));
        } catch (Throwable e2) {
            Log.i("DSTelephoneHacker", "couldn't hook method " + hMethods);
            DSLogger.error(e2, e2.getMessage(), new Object[0]);
        }
    }

    public void hookAll() {
    }

    protected void hookMethod(Class cls, String str, Object... objArr) {
        try {
            XposedHelpers.findAndHookMethod(cls, str, objArr);
        } catch (NoSuchMethodError e) {
            Log.i("DSTelephoneHacker", "couldn't hook method " + str);
        }
    }

    public void initData(Context context, com.alibaba.fastjson.JSONObject jSONObject) {
        DSLogger.info(" init data not implements yet:" + getClass(), new Object[0]);
    }

    protected void putMethodValues(Context context, JSONObject jSONObject, Object obj, HMethods[] hMethodsArr) {
        DSLogger.info(" init data : " + obj.getClass(), new Object[0]);
        for (HMethods hMethods : hMethodsArr) {
            DSLogger.info(" method :" + hMethods, new Object[0]);
            try {
                Object invoke = obj.getClass().getMethod(hMethods.name(), new Class[0]).invoke(obj, new Object[0]);
                if (!(invoke instanceof WifiInfo)) {
                    jSONObject.put(hMethods.name(), invoke);
                    DSLogger.info(" get res - " + hMethods + " :" + invoke, new Object[0]);
                }
            } catch (Exception e) {
                DSLogger.info(" error method [" + hMethods.name() + " ] for " + e.getMessage(), new Object[0]);
            }
        }
    }
}